package com.techventus.locations;

import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SettingsMenu extends Activity{
	TextView googleTitle;
	TextView googlesubTitle;
	LinearLayout googleLayout;
	TextView frequencyTitle;
	TextView frequencysubTitle;
	LinearLayout frequencyLayout;
	TextView deleteTitle;
	TextView deletesubTitle;
	LinearLayout deleteLayout;
	LinearLayout serviceLayout;
	
	LinearLayout resetLayout;
	LinearLayout powerLayout;
	
	TextView serviceStatus;
	
	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ); 
		setContentView(R.layout.settingsmenu);
		
		//Layout googleItem ;//= (Layout)findLayoutById(R.id.settingsmenugooglelayout);
		
		serviceLayout = (LinearLayout)findViewById(R.id.settingsmenubackgroundlayout);
		
		 googleTitle = (TextView)findViewById(R.id.settingsmenugoogletitle);
		 googlesubTitle = (TextView)findViewById(R.id.settingsmenugooglesubtext);
		 googleLayout = (LinearLayout)findViewById(R.id.settingsmenugooglelayout);
		//googleTitle.setOnClickListener(clickGoogle);
		//googlesubTitle.setOnClickListener(clickGoogle);
		googleLayout.setOnClickListener(clickGoogle);
		
		 frequencyTitle = (TextView)findViewById(R.id.settingsmenufrequenciestitle);
		 frequencysubTitle = (TextView)findViewById(R.id.settingsmenufrequenciessubtext);
		//frequencyTitle.setOnClickListener(clickFrequency);
		//frequencysubTitle.setOnClickListener(clickFrequency);
	//	frequencyLayout = (LinearLayout)findViewById(R.id.settingsmenufrequencieslayout);
	//	frequencyLayout.setOnClickListener(clickFrequency);

		
		deleteTitle = (TextView)findViewById(R.id.settingsmenudeletetitle);
	//deleteTitle.setOnClickListener(clickDelete);
		deleteLayout = (LinearLayout)findViewById(R.id.settingsmenudeletelayout);
		deleteLayout.setOnClickListener(clickDelete);
		resetLayout = (LinearLayout)findViewById(R.id.settingsmenuresetlayout);
		resetLayout.setOnClickListener(clickReset);
		
		powerLayout = (LinearLayout)findViewById(R.id.settingsmenupowerlayout);
		powerLayout.setOnClickListener(clickPower);
		
		
		serviceLayout.setOnClickListener(clickService);
		//BackgroundService
		//OK, No Connectivity
		serviceStatus = (TextView)findViewById(R.id.settingsmenubackgroundservicesubtext);
		
		
		//Google Account
		//OK, Log In Again
		
		//
		try{
			SQLiteDatabase sql = openOrCreateDatabase("db",0,null);
			Cursor c = sql.rawQuery("SELECT status FROM STATUS WHERE key = 'google';", null);
			
			if(c!=null){
				if(c.moveToNext()){
					googlesubTitle.setText(c.getString(0));
				}
				c.close();
			}
			
			 c = sql.rawQuery("SELECT status FROM STATUS WHERE key = 'service';", null);
				if(c!=null){
					if(c.moveToNext()){
						long time =(new Date()).getTime();
						
						long servicetime = Long.parseLong(c.getString(0));
						
						if(time-servicetime<300000){
							serviceStatus.setText("Service Functioning");
						}else{
							serviceStatus.setText("Service Error");
						}
					}
					c.close();
				}
			sql.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	@Override 
	public void onResume(){
		super.onResume();
		try{
			SQLiteDatabase sql = openOrCreateDatabase("db",0,null);
			Cursor c = sql.rawQuery("SELECT status FROM STATUS WHERE key = 'google';", null);
			if(c!=null){
				if(c.moveToNext()){
					googlesubTitle.setText(c.getString(0));
				}
				
	
				c.close();
			}
				
			 c = sql.rawQuery("SELECT status FROM STATUS WHERE key = 'service';", null);
				if(c!=null){
					if(c.moveToNext()){
						long time =(new Date()).getTime();
						
						long servicetime = Long.parseLong(c.getString(0));
						
						if(time-servicetime<300000){
							serviceStatus.setText("Service Functioning");
						}else{
							serviceStatus.setText("Service Error");
						}
					}
					c.close();
				}
					
			
	
			/*
			 c = sql.rawQuery("SELECT status FROM STATUS WHERE key = 'service';", null);
				if(c!=null){
					if(c.moveToNext()){
						googlesubTitle.setText(c.getString(0));
					}
					c.close();
				}*/
			sql.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	OnClickListener clickReset = new OnClickListener(){
		@Override
		public void onClick(View arg0) {
			System.out.println("Reset Confirm");
			Intent i = new Intent(SettingsMenu.this,ResetConfirm.class);
			startActivity(i);
			
		}
	};
	
	OnClickListener clickPower = new OnClickListener(){
		@Override
		public void onClick(View arg0) {
			System.out.println("Power Menu");
			Intent i = new Intent(SettingsMenu.this,PowerMode.class);
			startActivity(i);
			
		}
	};
	
	OnClickListener clickDelete = new OnClickListener(){

		@Override
		public void onClick(View arg0) {
			System.out.println("Delete Menu");
			Intent i = new Intent(SettingsMenu.this,DeleteLocation.class);
			startActivity(i);
			
		}};
	
	OnClickListener clickGoogle = new OnClickListener(){

		@Override
		public void onClick(View arg0) {
			Intent i = new Intent(SettingsMenu.this,LoginCredentials.class);
			startActivity(i);
			
		}};
	OnClickListener clickFrequency = new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				Intent i = new Intent(SettingsMenu.this,FrequencyMenu.class);
				startActivity(i);
				
			}};
			
	OnClickListener clickService = new OnClickListener(){

				@Override
				public void onClick(View arg0) {
					Intent i = new Intent(SettingsMenu.this,LocationService.class);
					startService(i);
					
			}};	
			
			
}
